package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chentengfei
 * @desc 菜单管理模块
 * @date 2023-06-11 19:59:05
 */
@RestController
@RequestMapping("/admin/acl/permission")
@Api(tags = "菜单管理")
// @CrossOrigin //跨域
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("查询所有菜单")
    @GetMapping
    /***
     * 查询所有菜单
     * //    url: `${api_name}`,
     * //    method: 'get'
     * @author chentengfei
     * @date 2023/6/11 20:08
     * @return com.atguigu.ssyx.common.result.Result
     */
    public Result list() {
        List<Permission> list = permissionService.queryAllPermission();
        return Result.ok(list);
    }

    @ApiOperation("添加菜单")
    @PostMapping("save")
    /***
     * 添加菜单
     * //    url: `${api_name}/save`,
     * //    method: "post",
     * //    data: permission
     * @author chentengfei
     * @date 2023/6/11 20:06
     * @param permission
     * @return com.atguigu.ssyx.common.result.Result
     */
    public Result list(@RequestBody Permission permission){
        permissionService.save(permission);
        return Result.ok(null);
    }

    /***
     * 修改菜单
     * //    url: `${api_name}/update`,
     * //    method: "put",
     * //    data: permission
     * @author chentengfei
     * @date 2023/6/11 20:07
     * @param permission
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.ok(null);
    }

    /***
     * 递归删除菜单
     * //    url: `${api_name}/remove/${id}`,
     * //    method: "delete"
     * @author chentengfei
     * @date 2023/6/11 20:07
     * @param id
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("递归删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        permissionService.removeChildById(id);
        return Result.ok(null);
    }
}
